<?php
class Duyethosocanhan extends MY_Controller {
 
    /**
    * Responsable for auto load the model
    * @return void
    */
    public function __construct(){
		parent::__construct();
		$this->load->model('canhan_m', 'canhan');
		$this->load->model('album_m', 'album');
		if(!$this->session->userdata('admin'))
			redirect(base_url('admin/login'));
    }
 
    /**
    * Load the main view with all the current model model's data.
    * @return void
    */
   public function index(){      
		$data = array();			
		//pagination settings
		$config['per_page'] = 10;
		$config['base_url'] = base_url('admin/duyethosocanhan');
		$config['use_page_numbers'] = TRUE;
		$config['num_links'] = $this->config->item('num_links');
		$config['full_tag_open'] = '<ul>';
		$config['full_tag_close'] = '</ul>';
		$config['num_tag_open'] = '<li>';
		$config['num_tag_close'] = '</li>';
		$config['cur_tag_open'] = '<li class="active"><a>';
		$config['cur_tag_close'] = '</a></li>';		
		$paged = $this->uri->segment(3);
		$paged = ($paged * $config['per_page']) - $config['per_page'];
		if ($paged < 0){
			$paged = 0;			
		}
		//filter post data
		$post = $this->input->post();
		if($post){
			$this->form_validation->set_rules('ho_ten', 'ho_ten', 'trim');
			$this->form_validation->set_rules('email', 'email', 'trim');
			$this->form_validation->set_rules('trang_thai', 'trang_thai', 'trim');
			$this->form_validation->run();
			$filter['ho_ten']	=	$this->input->post('ho_ten');
			$filter['email']	=	$this->input->post('email');
			$filter['trang_thai']	=	$this->input->post('trang_thai');
			$this->session->set_userdata('admin_filter', $filter);
		}else if($paged == 0)
			$this->session->unset_userdata('admin_filter');
		$tmp = array(
			'limit'		=>	$config['per_page'],
			'paged'		=>	$paged,			
		);
		//filter
		$filter = $this->session->userdata('admin_filter');
		if(!$filter) $filter = array();
		$filter = array_merge($filter, $tmp);
		//
		$config['total_rows'] = $this->canhan->get_hoso($filter, true);
		// echo $this->db->last_query();die;
		$data['hoso']		= $this->canhan->get_hoso($filter);
		$page = $this->uri->segment(3);
		$page = $page > 1 ? $page - 1 : 0;
		$data = array_merge($data, $filter);
		$data['stt']		= $page * $config['per_page'] + 1;		
		//initializate the panination helper 
      $this->pagination->initialize($config);  
		//load the view
      $data['main_content'] = 'admin/duyethoso/canhan';
      $this->load->view('admin/includes/template', $data);
   }
	
	function update(){
		$data = array();
		$hoso = $this->input->post('hoso');
		$status = $this->input->post('status');
		#echo '<pre>';print_r($post);die;
		if($hoso){
			$tmp = array();			
			foreach($hoso as $value)
				$tmp[]	=	array('id'=>$value,'trang_thai'=>$status,'ngay_duyet'=>date('Y-m-d H:i:s'));
			$updated = $this->canhan->cap_nhat_trang_thai($tmp);
			$this->session->set_flashdata('updated', 'Cập nhật trạng thái thành công');
		}else
			$this->session->set_flashdata('danger', 'Cập nhật trạng thái không thành công');
		//load the view
      redirect(base_url('admin/duyethosocanhan'));
	}
	
	function canhan(){
		$data = array();
		$id = $this->uri->segment(4);
		if($id){
			$data['canhan']	=	$this->canhan->get_canhan($id, true);
			if(!$data['canhan'])
				redirect('admin');
			$this->load->model('dichvu_m', 'dichvu');
			$data['dichvu_dk']	=	$this->dichvu->get_dichvu_dangky();
			$data['album']	=	$this->album->get_album_by_canhan_id($id);					
			// echo $this->db->last_query();die;
		}else{
			redirect('admin');
		}
		$data['main_content'] = 'admin/duyethoso/xemhscanhan';
      $this->load->view('admin/includes/template', $data);
	}
	
	function duyet(){
		$data = array();
		$id = $this->uri->segment(4);
		if($id){
			$canhan	=	$this->canhan->get_canhan($id, true);
			$tmp	= array();
			$tmp['id']	= $id;
			$tmp['trang_thai']	= 1;
			$tmp['ngay_duyet']	= date('Y-m-d H:i:s');
			$this->canhan->cap_nhat_trang_thai(array($tmp), true);			
			$tmp = array();
			$tmp['ngay_cap_nhat']	= $canhan->ngay_cap_nhat;			
			$canhan		= $this->canhan->get_hoso($tmp);
			if($canhan)
				$id = $canhan->id;
			else
				$id	= false;
			// echo $this->db->last_query();die;
		}else
			redirect(base_url('admin/duyethosocanhan'));
		if($id)
			redirect(base_url('admin/duyethosocanhan/canhan/'.$id));
		else	
			redirect(base_url('admin/duyethosocanhan'));
	}
	function khongduyet($id = false){
		if(!$id) redirect('admin/duyethosocanhan');
		$data = array();
		$canhan	=	$this->canhan->get_canhan_email($id);
		if($canhan){
			$data['canhan'] = $canhan;
		}else
			redirect('admin/duyethosocanhan');
		$data['main_content'] = 'admin/duyethoso/khongduyet';
      $this->load->view('admin/includes/template', $data);
	}
	function active($id = false){
		if(!$id) redirect('admin/duyethosocanhan');
		$data = array();
		$this->load->model('users_m', 'user');
		$user	=	$this->user->active($id);
		$this->session->set_flashdata('updated', 'Active thành công');
		redirect('admin/duyethosocanhan');
	}
	
	function deactive($id = false){
		if(!$id) redirect('admin/duyethosocanhan');
		$data = array();
		$this->load->model('users_m', 'user');
		$user	=	$this->user->deactive($id);
		$this->session->set_flashdata('updated', 'Deactive thành công');
		redirect('admin/duyethosocanhan');
	}
}